// pages/work/work.js
const db = wx.cloud.database()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    name:'',  //任务名称
    rules:[], //标注规则
    reward:[], //奖励方法
    BZtypechValue1:'',
    BZtypeenValue1:'',
    BZtypechValue2:'',
    BZtypeenValue2:'',
    BZtypechValue3:'',
    BZtypeenValue3:'',
    BZtypechValue4:'',
    BZtypeenValue4:'',
    rulesValue:'',
    rewardValue:'',
    BZtype: [], //标注类型
    Videoname:'',//演示视频名称
    Gotoname:'',//调用函数名称
    bkcolor:'white',//完成的背景色

  },

  //获取名称
  nameBlur(e){
    this.data.name = e.detail.value
  },
  //获取中文标注类型1
  typechBlur1(e){
    this.data.BZtypechValue1 = e.detail.value
  },
  //获取英文标注类型1
  typeenBlur1(e){
    this.data.BZtypeenValue1 = e.detail.value
  },
  //获取中文标注类型2
  typechBlur2(e){
    this.data.BZtypechValue2 = e.detail.value
  },
  //获取英文标注类型2
  typeenBlur2(e){
    this.data.BZtypeenValue2 = e.detail.value
  },  //获取中文标注类型3
  typechBlur3(e){
    this.data.BZtypechValue3 = e.detail.value
  },
  //获取英文标注类型3
  typeenBlur3(e){
    this.data.BZtypeenValue3 = e.detail.value
  },  
  //获取中文标注类型4
  typechBlur4(e){
    this.data.BZtypechValue4 = e.detail.value
  },
  //获取英文标注类型4
  typeenBlur4(e){
    this.data.BZtypeenValue4 = e.detail.value
  },  

  //获取任务规则
  rulesBlur(e){
    this.data.rulesValue = e.detail.value
    this.data.rules = e.detail.value.split('\n')
  },
  //获取奖励办法
  rewardBlur(e){
    this.data.rewardValue = e.detail.value
    this.data.reward = e.detail.value.split('\n')
  },
  //下一个数据输入
  nextone(){
    let BZtypeItem = {}
    BZtypeItem.content = this.data.BZtypechValue
    BZtypeItem.english_con = this.data.BZtypeenValue
    BZtypeItem.clicked = false
    console.log(BZtypeItem)
    this.data.BZtype.push(BZtypeItem)
    this.setData({
      BZtypechValue:'',
      BZtypeenValue:'',
    })
  },
  //类型输入完成
  getall(){
    //第一个标注类型
    if(this.data.BZtypechValue1!=''){
      let BZtypeItem = {}
      BZtypeItem.content = this.data.BZtypechValue1
      BZtypeItem.english_con = this.data.BZtypeenValue1
      BZtypeItem.clicked = false
      this.data.BZtype.push(BZtypeItem)
      this.setData({
        bkcolor:'lightcolor'
      })
    }
    //第二个标注类型
    if(this.data.BZtypechValue2!=''){
      let BZtypeItem = {}
      BZtypeItem.content = this.data.BZtypechValue2
      BZtypeItem.english_con = this.data.BZtypeenValue2
      BZtypeItem.clicked = false
      this.data.BZtype.push(BZtypeItem)
    }
    //第三个标注类型
    if(this.data.BZtypechValue3!=''){
      let BZtypeItem = {}
      BZtypeItem.content = this.data.BZtypechValue3
      BZtypeItem.english_con = this.data.BZtypeenValue3
      BZtypeItem.clicked = false
      this.data.BZtype.push(BZtypeItem)
    }
    //第四个标注类型
    if(this.data.BZtypechValue4!=''){
      let BZtypeItem = {}
      BZtypeItem.content = this.data.BZtypechValue4
      BZtypeItem.english_con = this.data.BZtypeenValue4
      BZtypeItem.clicked = false
      this.data.BZtype.push(BZtypeItem)
    }
  },

  //获取视频名称
  VideonameBlur(e){
    this.data.Videoname = e.detail.value
  },

  //获取函数名称
  GotonameBlur(e){
    this.data.Gotoname = e.detail.value
  },
  // 添加或者修改worktasks数据
  addorupdate() {
    var that = this
      if (that.data.name == ''){
        wx.showModal({
          title: '温馨提醒',
          content: '请输入任务名称！',
          showCancel: false
        })
      }
      else if (that.data.rules.length == 0) {
        wx.showModal({
          title: '温馨提醒',
          content: '请输入任务规则！',
          showCancel: false
        })
      }
      // else if (that.data.BZtype.length == 0) {
      //   wx.showModal({
      //     title: '温馨提醒',
      //     content: '请输入标注类型',
      //     showCancel: false
      //   })
      // }
      else if (that.data.rules.reward == 0) {
        wx.showModal({
          title: '温馨提醒',
          content: '请输入奖励办法',
          showCancel: false
        })
      }
      // else if (that.data.Videoname == '') {
      //   wx.showModal({
      //     title: '温馨提醒',
      //     content: '请输入视频名称',
      //     showCancel: false
      //   })
      // }
      else if (that.data.Gotoname == '') {
        wx.showModal({
          title: '温馨提醒',
          content: '请输入函数名称',
          showCancel: false
        })
      }
      else{
        db.collection('worktasks')
        .where({
          name:that.data.name
        })
        .get()
        .then(res=>{
          console.log(res.data.length)
          if(res.data.length == 0){
            db.collection('worktasks').add({
              data: {
                // block:wx.getStorageSync('block'),
                rwname:that.data.name,
                BZtype:that.data.BZtype,
                rules:that.data.rules,
                reward:that.data.reward,
                BZdemo:that.data.Videoname,
                hsm:that.data.Gotoname
              }
            })
            .then(res => {
              wx.showModal({
                title: '恭喜',
                content: '任务添加成功！',
                showCancel: false,
                complete() {
                  // 跳转到index页面
                  wx.redirectTo({
                    url: '/pages/index/index'
                  })
                }
              })
            })

          }else{
            wx.showModal({
              title: '提醒',
              content: '该任务已添加',
              showCancel: false,
            })
          }
        })

      }
  },

  // 上传
  sumit(){
    this.addorupdate()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this

  },

})